using System;
using System.Collections.Generic;
using System.Text;
using DataBase.Core;
using System.Data;
using Net.RequestHandle;

namespace MeetFuture.MRC
{
	public partial class clsAGVList
	{
        private const string key_split = ";";

        Dictionary<string, clsAGVInfo> _AGVList = new Dictionary<string, clsAGVInfo>();

        public clsAGVList()
        {
            this._AGVList = new Dictionary<string, clsAGVInfo>();
        }

        public Dictionary<string, clsAGVInfo> AGVList
        {
            get { return this._AGVList; }
        }
        public void Add(clsAGVInfo AGV)
        {
            try
            {
                clsAGVInfo tmpAGV = null;
                this._AGVList.TryGetValue(AGV.AGVID, out tmpAGV);
                if (tmpAGV == null)
                {
                    this._AGVList.Add(AGV.AGVID, AGV);
                } 
                else
                {
                    this._AGVList[AGV.AGVID] = AGV;
                }                
            }
            catch (System.Exception ex)
            {	
            }            
        }
        public clsAGVInfo FindAgvByAgvID(string AgvId)
        {
            clsAGVInfo result = null;
            try
            {
                this._AGVList.TryGetValue(AgvId, out result);
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }
        public void Remove(clsAGVInfo AGV)
        {
            try
            {
                this._AGVList.Remove(AGV.AGVID);
            }
            catch (System.Exception ex)
            {	
            }
        }
        public void Remove(string AGV_ID)
        {
            try
            {
                this._AGVList.Remove(AGV_ID);
            }
            catch (System.Exception ex)
            {	
            }
        }        
        public clsAGVInfo FindTool(string ToolId)
        {
            clsAGVInfo result = null;
            try
            {
                this._AGVList.TryGetValue(ToolId, out result);
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }
        public bool Find_AGV_By_Lot(string LotID, out clsAGVInfo Out_AGV)
        {
            bool result = false;
            Out_AGV = null;
            try
            {
                foreach (clsAGVInfo agvinfo in this._AGVList.Values)
                {
                    foreach (clsAGVCommand cmd in agvinfo.AGV_Commands)
                    {
                        foreach (clsLotInfo lot in cmd.LotList.Values)
                        {
                            if (lot.LotID == LotID)
                            {
                                Out_AGV = agvinfo;
                                result = true;
                                return result;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }
        public clsAGVInfo Find_AGV_By_Socket(System.Net.Sockets.Socket socket)
        {
            clsAGVInfo result = null;
            try
            {
            	foreach (clsAGVInfo agvinfo in this._AGVList.Values)
            	{
                    if (agvinfo.AGV_Socket.socket != null)
                    {
                        if (agvinfo.AGV_Socket.socket.Equals(socket))
                        {
                            result = agvinfo;
                            break;
                        }
                    }            	    
            	}
            }
            catch (System.Exception ex)
            {
                result = null;
            }
            return result;
        }
        public string ToObjectString()
        {
            string result = string.Empty;
            try
            {
                result += "[";
                foreach (clsAGVInfo agv in this._AGVList.Values)
                {
                    result += agv.ToObjectString() + key_split;
                }
                result = result.Trim(key_split.ToCharArray()) + "]";
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }
	}
}
